﻿#pragma once

#include "CTsmpBag.h"
#include "CIParaWrapper.h"
#include "CEParaWrapper.h"
#include "CGrpInfWrapper.h"
#include "CTprgInfWrapper.h"
#include "CResInfWrapper.h"
#include "CIdtInfWrapper.h"
#include "CAmtInfWrapper.h"
#include "CCptInfWrapper.h"
#include "CStdInfManager.h"
#include "AnalExecutor.h"

#define X7F 0x7F
#define X80 0x80
#define C32 ' '
#define C36 '$'
#define C66 'B'
#define C70 'F'
#define C78 'N'
#define C80 'P'
#define C83 'S'
#define C84 'T'
#define C86 'V'
#define C88 'X'
#define C115 's'
#define C116 't'

class AnalysisPool
{
public:
    AnalExecutor *pAnalExecutor;      // v0
    CTsmpBag *pTsmpBag;               // v1
    CIParaWrapper *pIParaWrapper;     // v2
    CTprgInfWrapper *pTprgInfWrapper; // v3
    CResInfWrapper *pResInfWrapper;   // v4
    CEParaWrapper *pEParaWrapper;     // v5
    CGrpInfWrapper *pGrpInfWrapper;   // v6
    CIdtInfWrapper *pIdtInfWrapper;   // v7
    CAmtInfWrapper *pAmtInfWrapper;   // v8
    CCptInfWrapper *pCptInfWrapper;   // v9
    CStdInfManager *pStdInfManager;   // v10
    int32_t isOpened;
    int32_t isStarted;

    // sub_1001D700
    AnalysisPool();

    void printSelf(char *captionUpper, int32_t deep);

    // sub_10016B76
    int32_t startAnal();

    // sub_1001690E
    int32_t closeAnal();

    // sub_10016933
    AnalysisPool *closeAnalPool();

    // sub_10016401
    int32_t openAnal(TSMPBAG *smpBag, TDLLIPINF *ipInf, TDLLEPINF *epInf, TDLLIDTINF *idtInf, TDLLRESINF *resInf,
                     TDLLSTDINF *stdInf);

    // sub_10016CCC
    char *getErrMsg();

    // sub_10016BAC
    int32_t stopAnal();

    // sub_10016C10
    CODE execAnal();

    // sub_1001D7C0
    int32_t getAnalTime();

    // sub_10016C62
    int32_t setStopTime(float stopTime);

    // sub_10016C97
    int32_t setPeakSmpHead(int32_t *pSmpHead);
};

extern AnalysisPool *gAnalPoolList;

int32_t InitAnalPool_(int32_t maxChannels, int32_t realChannels,
                      void(__cdecl *drawTickMark)(int32_t, int32_t, TICKMARK));
int32_t ExitAnalPool_();
int32_t GetAnalPoolVolume_(int32_t *maxReal, int32_t *maxRedo);
int32_t OpenAnal_(int32_t chn, TSMPBAG *smpBag, TDLLIPINF *ipInf, TDLLEPINF *epInf, TDLLIDTINF *idtInf,
                  TDLLRESINF *resInf, TDLLSTDINF *stdInf = NULL);
int32_t CloseAnal_(int32_t chn);
int32_t StartAnal_(int32_t chn);

int32_t StopAnal_(int32_t chn);
CODE ExecAnal_(int32_t chn);
int32_t GetAnalTime_(int32_t chn);
char *GetErrMsg_(int32_t chn);
int32_t MdyAnalStopTime_(int32_t chn, float stopTime);
int32_t MdyPeakSmpHead_(int32_t chn, int32_t *pSmpHead);
int32_t TickAnal_(int32_t chn);
int32_t AnalOpened_(int32_t chn);
int32_t AnalBusy_(int32_t chn);
int32_t GetAnalOpened_(int32_t **chArray);
int32_t GetAnalBusy_(int32_t **chArray);
int32_t SetUkPkFactor_(int32_t chn, TUkPkFactor *lpUkPkFactor);
int32_t SetIdtTimeOfs_(int32_t chn, float fTimeOfs);
int32_t SetYMinUnit_(int32_t chn, double yMinUnit);
int32_t SetIdtQualitMode_(int32_t chn, int32_t nQualitMode, float fTimeWindow);
int32_t SetXMinUnitK_(int32_t chn, double xMinUnitK);
int32_t SetMiscellaneous_(int32_t chn, int64_t wParam, int64_t lParam);
int32_t EvalResult_(TDLLRESINF *resInf, TDLLEPINF *epInf, TDLLIDTINF *idtInf, TDLLMETRIC *metric);
int32_t EvalColumnPerfEx_(TDLLResultListItem *pHead, int32_t pCount, int32_t *smpHead, float freq, double xMinUnitK);
double EvalKHeightWEx_(TDLLResultListItem *pHead, int32_t *smpHead, double k, float freq, double xMinUnitK,
                       double *lpWL, double *lpWR);
int32_t EvalColumnPerf_(TDLLResultListItem *pHead, int32_t pCount, int32_t *smpHead, float freq);
int32_t EvalColumnPerfEx_(TDLLResultListItem *pHead, int32_t pCount, int32_t *smpHead, float freq, float time);
double EvalKHeightW_(TDLLResultListItem *pHead, int32_t *smpHead, double k, float freq, double *lpWL, double *lpWR);
int32_t EvalGroup_(TDLLGRPINF *grpInf, TDLLRESINF *resInf);
int32_t EvalGroupResult_(TDLLGROUPListItemMerged *pItem, TDLLGRPINF *pGrpInf);
int32_t EvalCalibFact_(TDLLSTDINF *stdInf, TEPara *epara, TDLLIDTINF *idtInf);
int32_t ManualPeakStart_(int32_t chn, float startTime);
int32_t ManualPeakEnd_(int32_t chn, float endTime);
int32_t ManualPeakMoveSplit_(int32_t chn, float valleyTime);
int32_t ManualPeakAddSplit_(int32_t chn, float splitTime);
int32_t ManualPeakDelSplit_(int32_t chn, float splitTime);
int32_t ManualPeakAddOnlyOne_(int32_t chn, float beginTime, float endTime);
int32_t ManualPeakDelete_(int32_t chn, float beginTime, float endTime);
int32_t ManualBaseValley_(int32_t chn, float beginTime, float endTime);
int32_t ManualBaseTogether_(int32_t chn, float beginTime, float endTime);
int32_t ManualBaseTailTangent_(int32_t chn, float beginTime, float endTime);
int32_t ManualBaseFrontTangent_(int32_t chn, float beginTime, float endTime);
int32_t ManualBaseForwHori_(int32_t chn, float beginTime, float endTime);
int32_t ManualBaseBackHori_(int32_t chn, float beginTime, float endTime);
int32_t ManualParaWidth_(int32_t chn, float beginTime, float endTime, float pkWidth);
int32_t ManualParaNoise_(int32_t chn, float beginTime, float endTime, float pkNoise);
double CvtConcToAmt_(double conc, TDLLMETRIC *metric, int32_t method);
double CvtConcToAmtByIstd_(double conc, TDLLMETRIC *metric);
int32_t LearnAutoPara_(AUTOPOOL *autoPool, float freq, AUTOPARA *autoPara);
int32_t MakeAutoPara_(int32_t *buf, int32_t len, float freq, AUTOPARA *autoPara);
int32_t LearnAutoIPara_(AUTOPOOL *autoPool, float freq, AUTOIPARA *autoIPara);
int32_t MakeAutoIPara_(int32_t *buf, int32_t len, float freq, AUTOIPARA *autoIPara);

// sub_1001956B
int32_t calcNoise(int32_t *data, int32_t tail, int32_t chn);

extern TSMPBAG *old_smpBag[100];
extern TDLLIPINF *old_ipInf[100];
extern TDLLEPINF *old_epInf[100];
extern TDLLIDTINF *old_idtInf[100];
extern TDLLRESINF *old_resInf[100];
extern TDLLSTDINF *old_stdInf[100];